<template>
	<view>
		<view class="title-bar">
				<text class="sign-up-link" @click="toSignUp">注册</text>
		</view>
		<view class="sign-in-box">
			<view class="sign-in-title">
				欢迎来到一起平台
			</view>
			<u-input class="cinput"
				:class="{error: isAcError}"
				v-model="account"
				:custom-style="inputStyle"
				height="119"
				type="text"
				:placeholder-style="isAcError?'color:#fa3534;':'color:#c0c4cc;'"
				@focus="clearErr"
				:placeholder="`请输入${loginType == 0? '账号': '手机号'}`" />
			<u-input class="cinput"
				:class="{error: isPwdError}"
				v-model="password"
				:custom-style="inputStyle"
				height="119"
				type="password"
				:placeholder-style="isPwdError?'color:#fa3534;':'color:#c0c4cc;'"
				@focus="clearErr"
				placeholder="请输入密码" v-if="loginType == 0" />
			<view class="sign-in-bar">
				<view>
					<text class="sign-in-type" v-if="loginType == 0" @click="loginType = 1">短信验证码登录</text>
					<text class="sign-in-type" v-else @click="loginType = 0">账号密码登录</text>
				</view>
				<view>
					<u-button :custom-style="btnStyle" v-if="loginType == 0" shape="circle" @click="signIn">登录</u-button>
					<u-button :custom-style="btnStyle" v-else shape="circle" @click="getCode">获取验证码</u-button>
				</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import md5Libs from "uview-ui/libs/function/md5";
	
	export default {
		data() {
			return {
				inputStyle: {
					fontSize: '36rpx'
				},
				btnStyle: {
					width: '200rpx',
					height: '100rpx',
					color: '#FFFFFF',
					background: 'linear-gradient(90deg, #22ABEC 0%, #0071C5 100%)'
				},
				account: '',
				password: '',
				isAcError: false,
				isPwdError: false,
				loginType: 0
			}
		},
		methods: {
			// 自动获取焦点,清楚错误提示
			clearErr() {
				this.isAcError = false
				this.isPwdError = false
			},
			signIn() {
				if (this.account === '') {
					this.isAcError = true
				} else if (this.password === '') {
					this.isPwdError = true
				} else {
					this.$u.api.signIn('password', {account: this.account, password: md5Libs.md5(this.password)}).then(res => {
						this.$u.vuex('vuex_token', res.token)
						this.$u.api.getCurrentUser().then(res => {
							console.log("登录存取vuex的数据",res)
							this.$u.vuex('vuex_user', res)
							if (!this.$store.state.socket_state) {
								this.$socket.initWebSocket()
							}
							console.log(res)
							this.$u.route({type: 'tab', url: 'pages/home/home'})
						}).catch(err => {
							console.error(err)
						})
					}).catch(err => {
						console.error(err)
					})
				}
			},
			toSignUp() {
				this.$u.route('/pages/users/sign-up')
			},
			getCode() {
				if (this.account === '') {
					this.isAcError = true
				} else {
					const _guid = this.$u.guid()
					this.$u.api.sendLoginCaptcha({phone: this.account, uuid: _guid}).then(res => {
						this.$u.route({
							type: 'redirect',
							url: 'pages/users/code',
							params: {phone: this.account, guid: _guid}
						})
					}).catch(err => {
						console.error(err)
					})
				}
			}
		},
		onReady() {
			// #ifdef APP-PLUS
			plus.navigator.setStatusBarStyle('dark')
			// #endif
		}
	}
</script>

<style lang="scss" scoped>
	.title-bar {
		padding-top: var(--status-bar-height);
		height: 80rpx;
		text-align: right;
		.sign-up-link {
			display: inline-block;
			width: 160rpx;
			height: 100rpx;
			line-height: 80rpx;
			text-align: center;
			font-size: 28rpx;
			font-weight: 500;
			color: #0272C5;
		}
	}
	.sign-in-box {
		padding: 60rpx;
		.sign-in-title {
			padding: 60rpx 0;
			font-size: 60rpx;
			font-weight: 500;
		}
		.sign-in-bar {
			display: flex;
			justify-content: space-between;
			margin-top: 60rpx;
			.sign-in-type {
				line-height: 100rpx;
				font-size: 28rpx;
				color: #999999;
			}
		}
	}
	.cinput {
		border-bottom: 1px solid;
	}
	.cinput.error {
		border-bottom-color: #fa3534 !important;
	}
</style>
